package Iostart;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class WenjianFile {
	public static void main(String[] args) {
		File lest01 = new File("D:\\a");
		File lest02 = new File("D:\\b");
		int count = 0;
		// tree(lest01, count);
		System.out.println("===============");
		digui(lest01, lest02, count);
	}

	static void digui(File aa, File bb, int count) {

		String str = "";

		FileInputStream fis = null;
		FileOutputStream fos = null;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;

		StringBuilder rest = new StringBuilder();
		for (int i = 0; i < count; i++) {
			rest.append("|-->");
		}

		System.out.println(rest.toString() + aa.getName());

		File[] lestOne = aa.listFiles();

		for (int i = 0; i < lestOne.length; i++) {

			if (!lestOne[i].isDirectory()) {
				try {
					fis = new FileInputStream(lestOne[i]);
					bis = new BufferedInputStream(fis);
					str = bb.getPath() + "\\" + lestOne[i].getName();
					File f = new File(str);
					f.createNewFile();
					fos = new FileOutputStream(f);
					bos = new BufferedOutputStream(fos);

					byte[] b = new byte[1024];

					int len = 0;
					
					while ((len = bis.read(b)) != -1) {
						bos.write(b, 0, len);
					}
					bos.flush();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if (bos != null) {
							bos.close();
							bos = null;
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
					try {
						if (bos != null) {
							bos.close();
							bos = null;
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			} else {
				str = bb.getPath() + "\\" + lestOne[i].getName();
				File ff = new File(str);
				ff.mkdir();
				digui(lestOne[i], ff, count + 1);
			}
		}
	}

}
